package com.internetCafes.spms.web.customer.bizservice.impl.customer;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.internetCafes.spms.common.utils.ShiroUtils;
import com.internetCafes.spms.common.utils.entity.page.PageItemDTO;
import com.internetCafes.spms.common.utils.entity.page.PageItemUtil;
import com.internetCafes.spms.common.utils.entity.page.PageItemVO;
import com.internetCafes.spms.core.exception.RRException;
import com.internetCafes.spms.core.exception.RRExceptionCodeEnum;
import com.internetCafes.spms.web.customer.bizservice.customer.IEntCustomerMaterialBizService;
import com.internetCafes.spms.web.customer.entity.customer.Customer;
import com.internetCafes.spms.web.customer.entity.customer.CustomerMaterial;
import com.internetCafes.spms.web.customer.model.customer.material.MaterialPagingDo;
import com.internetCafes.spms.web.customer.model.customer.material.MaterialPagingReq;
import com.internetCafes.spms.web.customer.model.customer.material.MaterialPagingRes;
import com.internetCafes.spms.web.customer.model.customer.material.MaterialSaveReq;
import com.internetCafes.spms.web.customer.service.customer.ICustomerMaterialService;
import com.internetCafes.spms.web.customer.service.customer.ICustomerService;
import com.sms.common.util.hutool.core.bean.BeanUtil;
import com.sms.common.util.hutool.core.date.DatePattern;
import com.sms.common.util.hutool.core.date.DateUtil;
import com.sms.common.util.hutool.core.util.ObjectUtil;
import com.sms.common.util.hutool.core.util.StrUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class EntCustomerMaterialBizServiceImpl implements IEntCustomerMaterialBizService {

    @Autowired
    private ICustomerMaterialService materialService;
    @Autowired
    private ICustomerService customerService;

    @Override
    public PageItemVO<MaterialPagingRes> paging(PageItemDTO<MaterialPagingReq> req) {
        Page<CustomerMaterial> pagingDo = new Page<>(req.getPageNum(), req.getPageSize());
        MaterialPagingReq conditions = req.getConditions();
        MaterialPagingDo materialPagingDo = new MaterialPagingDo();
        if (ObjectUtil.isNotNull(materialPagingDo)) {
            BeanUtil.copyProperties(conditions, materialPagingDo);
        }
        Page<CustomerMaterial> pagingPo = materialService.paging(pagingDo, materialPagingDo);
        PageItemVO<MaterialPagingRes> pagingRes = PageItemUtil.toPageItemVO(pagingPo, new PageItemVO<>());
        pagingRes.setRecords(
                pagingPo.getRecords()
                        .stream()
                        .map(po -> {
                            MaterialPagingRes res = new MaterialPagingRes();
                            BeanUtil.copyProperties(po, res);
                            res.setCreateTime(DateUtil.format(po.getCreateTime(), DatePattern.NORM_DATETIME_PATTERN));
                            return res;
                        })
                        .collect(Collectors.toList()));
        return pagingRes;
    }

    @Override
    public boolean delete(List<Long> idList) {
        return materialService.removeByIds(idList);
    }

    @Override
    public boolean save(MaterialSaveReq req) {
        if (StrUtil.isBlank(req.getName()) || StrUtil.isBlank(req.getFileUrl()) || ObjectUtil.isNull(req.getCustomerId())) {
            throw new RRException(RRExceptionCodeEnum.PARAM_ERROR);
        }

        // 获取客户信息
        Customer customerPo = customerService.getById(req.getCustomerId());
        if (ObjectUtil.isNull(customerPo)) {
            throw new RRException(RRExceptionCodeEnum.DATA_NOT_EXIST);
        }

        // 查看当前用户是否有权限新增资料
        if (!ShiroUtils.getUserId().equals(customerPo.getFollowUserId())) {
            throw new RRException(RRExceptionCodeEnum.NO_AUTHORITY);
        }

        CustomerMaterial materialDo = new CustomerMaterial();
        BeanUtil.copyProperties(req, materialDo);

        return materialService.save(materialDo);
    }
}
